package com.powernode.spring6.service.Impl;

import com.powernode.spring6.dao.AccountDao;
import com.powernode.spring6.pojo.Account;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

@Service("i2")
public class IsolationService2 {
    @Resource(name = "accountDao")
    private AccountDao accountDao;

    // 2号
    // 负责insert
    // @Transactional(timeout = 10,readOnly = true)// 设置事务超时时间 将事务设置为只读
    // @Transactional(rollbackFor = RuntimeException.class)// 遇到RuntimeException或者这个异常的子类异常就回滚
    @Transactional(noRollbackFor = NullPointerException.class)// 遇到空指针异常或者这个异常的子类就不回滚
    public void save(Account act) throws IOException {

        /*try {
            Thread.sleep(1000 * 15);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        accountDao.insert(act);

        if (1 == 1) {
            //throw new IOException();
            throw new RuntimeException();
            //throw new NullPointerException();
        }
        // 睡眠一会
        /*try {
            Thread.sleep(1000 * 15);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/

        // 当然，如果想让整个方法的所有代码都计入超时时间的话，可以在方法最后一行添加一行无关紧要的DML语句。
    }
}
